﻿using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Drawing;

namespace HomeWork4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.treeView1.NodeMouseClick += new TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
            LoadDirectories();
            this.listView1.ItemActivate += new EventHandler(this.listView1_ItemActivate);
            listView1.View = View.Details;
            listView1.Columns.Add("文件名", -2, HorizontalAlignment.Left);

            listView1.Columns.Add("类型", -2, HorizontalAlignment.Left);

            listView1.Columns.Add("修改日期", -2, HorizontalAlignment.Left);
        }

       

        private void GetDirectories(DirectoryInfo[] mydir, TreeNode mytonode)
        {
            TreeNode aNode;
            DirectoryInfo[] submydir;

            int mygoal = 0;
            foreach (DirectoryInfo subDir in mydir)
            {
                aNode = new TreeNode(subDir.Name, 0, 0);

                aNode.Tag = subDir;

                mygoal++;
                aNode.ImageKey = "folder";
                try
                {
                    submydir = subDir.GetDirectories();
                    if (submydir.Length != 0)
                    {
                        GetDirectories(submydir, aNode);
                    }
                }
                catch (Exception ex)
                {
                    // Catch here to handle restricted access folders exceptions just for sure
                }
                mytonode.Nodes.Add(aNode);
            }
        }
        private void LoadDirectories()
        {
            TreeNode orinode;

            DirectoryInfo info = new DirectoryInfo(@"C:/");
            if (info.Exists)
            {

                orinode = new TreeNode(info.Name);
                orinode.Tag = info;
                GetDirectories(info.GetDirectories(), orinode);
                treeView1.Nodes.Add(orinode);
            }
        }
        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode myselect = e.Node;
            listView1.Items.Clear();

            DirectoryInfo nodeDirInfo = (DirectoryInfo)myselect.Tag;

            ListViewItem.ListViewSubItem[] subItems;
            ListViewItem item = null;

            foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
            {
                item = new ListViewItem(dir.Name, 0);
                subItems = new ListViewItem.ListViewSubItem[]
                  {new ListViewItem.ListViewSubItem(item, "Directory"),

                   new ListViewItem.ListViewSubItem(item,
                dir.LastAccessTime.ToShortDateString())};

                item.SubItems.AddRange(subItems);
                listView1.Items.Add(item);
            }
            foreach (FileInfo file in nodeDirInfo.GetFiles())
            {
                item = new ListViewItem(file.Name, 1);
                subItems = new ListViewItem.ListViewSubItem[]
                  { new ListViewItem.ListViewSubItem(item, "File"),
                   
                      new ListViewItem.ListViewSubItem(item,
                file.LastAccessTime.ToShortDateString())};

                item.SubItems.AddRange(subItems);
                listView1.Items.Add(item);
            }

            listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
        }




        private void listView1_ItemActivate(object sender, EventArgs e)
        {

            string fullPath = Path.Combine(((DirectoryInfo)treeView1.SelectedNode.Tag).FullName, listView1.SelectedItems[0].Text);




            if (File.Exists(fullPath))
            {
                string ext = Path.GetExtension(fullPath);



                if (ext == ".exe")
                {
                    Process.Start(fullPath);
                }
                else if (ext == ".txt")
                {
                    Process.Start("notepad.exe", fullPath);
                }
            }



            else
            {
                MessageBox.Show("抱歉，该文件不属于.exe或者.txt类型，不能打开。");
            }
        }
        private void Form1_Load_1(object sender, EventArgs e)
        {

        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void 文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
